/**
* 系统管理 用户管理
*/
<template>


  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据清洗任务库</el-breadcrumb-item>
    </el-breadcrumb>

    <div style="padding:5px;color:#000000;text-align: center;font-family:PingFang SC;font-size:30px">数据清洗任务库列表</div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="">
        <el-input size="small" @keyup.enter="getList(formInline)"  v-model="formInline.taskName" placeholder="输入任务名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="getList(formInline)">搜索</el-button>
        <el-button size="small" type="info" icon="el-icon-refresh" @click="reset">重置</el-button>
        <el-button size="small" type="success" icon="el-icon-plus" @click="addTask">新增任务</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="dataCleanTaskList" highlight-current-row v-loading="loading" border element-loading-text="执行任务中" style="width: 100%;">
      <el-table-column v-if="false" align="center" type="selection" width="40">
      </el-table-column>
      <el-table-column align="center" sortable prop="id" v-if="false" label="序号" width="90">
      </el-table-column>
      <el-table-column align="center" prop="taskName" label="任务名称" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="sourceType" label="数据来源类型" width="150">
      </el-table-column>
      <el-table-column align="center" sortable prop="taskDesc" label="任务描述" width="150">
      </el-table-column>
      <el-table-column align="center" prop="status" label="状态" width="150">
      </el-table-column>
      <el-table-column align="center" v-if="false" sortable prop="stioreConfigId" label="存储配置id" width="90">
      </el-table-column>
      <el-table-column align="center" sortable prop="config" label="配置" width="150">
      </el-table-column>

      <!--操作-->
      <el-table-column align="center" label="操作" fixed="right" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="primary"@click="action(scope.row.status,scope.row.id)">立即执行</el-button>
          <el-button size="mini" type="success" @click="detail(scope.row.id)">详情</el-button>
          <!--<el-button size="mini" type="warning" @click="">修改</el-button>-->
          <el-button size="mini" type="danger" @click="deleteTask(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->

    <algo-config :visible.sync="visible"/>
  </div>
</template>

<script>
  // 导入请求方法

  import Pagination from '../../components/Pagination'
  import axios from "axios";

  import UnStructDataMan from "../dataManage/UnStructDataMan.vue";
  import AlgoConfig from "./algo-config.vue";
  export default {
    data() {
      return {

        dataCleanTaskList:[],
        // 删掉

        isShow:false,
        isShow1:false,

        loading: false, //是显示加载
        title: '新增任务',
        editFormVisible: false, //控制编辑页面显示与隐藏



        // 编辑与添加
        editForm: {
          id: '',
          taskName: '',
          modeltargets:[],
          example: '',
          sourceType: '',
          sourceId: '',
          datasourceIds:[],
          tagId:'' ,
          status:'未执行',
          userId: localStorage.getItem('userId')
        },
        // 选择数据
        selectdata: [],

        // 请求数据参数
        formInline: {
          page: 1,
          limit: 10,
          taskName: '',
          isLock: ''
        },


        // 分页参数
        Data:[],
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        },

        dialogVisible: false,
        checkedData: [],

        visible: false
      }
    },
    // 注册组件
    components: {
      Pagination,
      UnStructDataMan,
      AlgoConfig
    },

    /**
     * 数据发生改变
     */
    watch: {},

    /**
     * 创建完毕
     */
    created() {
      this.data1 = localStorage.getItem('userId')
      this.getList(this.formInline)

    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取数据列表
      getList(formInline){
        this.logining = true
        let url = this.$global.javaUrl +"/dataCleanTask/list"
        let config = {
          headers:
            {
              //'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            },
          params: {
            taskName:this.formInline.taskName,
            pageNum: this.formInline.page,
            pageSize:this.formInline.limit,

          }
        }

        axios.get(url, config).then(response=>{
          this.dataCleanTaskList = response.data.data.rows

          this.pageparm.total = response.data.data.records


          // const rows = response.data.data.rows;
          // const keys = Object.keys(rows);
          //
          // for (let i = 0; i < keys.length; i++) {
          //   console.log("i"+keys[i], rows[keys[i]]);
          // }

        })


      },
      // 详情
      detail(id){
        this.$router.push({path:'dataCleanTaskDetail',query:{id:id}});
      },
      // 删除任务
      deleteTask(id) {
        let config = {
          headers:
            {
              'Content-Type':'application/json',
              'user' : localStorage.getItem('userId')
            },
          params:{'id':id}

        }

        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            // 删除
            axios.get(this.$global.javaUrl+"/dataCleanTask/delete",config)
              .then(res => {
                if (res.data.data !=0) {
                  this.$message({
                    type: 'success',
                    message: '任务已删除!'
                  })
                  this.getList(this.formInline)
                } else {
                  this.$message({
                    type: 'info',
                    message: "1111"
                  })
                }
              })
              .catch(err => {
                this.$message({
                  type: 'error',
                  message: '任务删除失败，请稍后再试！'
                })
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除！'
            })
          })
      },

      // 执行清洗任务
      action(status,id) {
       if(status === '已执行'){
         this.$message({

           type: 'error',
           message: '该任务已执行，无需再次执行!'
         })
       }
       else{
         let url = this.$global.javaUrl + "/dataCleanTask/actionNow"
         let config = {
           headers:
             {
               'Content-Type':'application/json',
               'user' : localStorage.getItem('userId')
             },
           params:{'id':id}

         }

         this.$confirm('确定要执行该任务吗?', '信息', {
           confirmButtonText: '确定',
           cancelButtonText: '取消',
           type: 'warning'
         })
           .then(() => {
             this.loading = true;
             // 删除
             axios.get(url,config)
               .then(res => {
                 if (res.data.data !=0) {
                   this.loading = false;
                   this.$message({

                     type: 'success',
                     message: '任务执行成功!'
                   })
                   this.getList(this.formInline)
                 } else {
                   this.$message({
                     type: 'info',
                     message: "1111"
                   })
                 }
               })
               .catch(err => {
                 this.loading = false
                 this.$message.error('任务执行失败，请稍后再试！')
               })
             this.getList(this.formInline)
           })
           .catch(() => {
             this.$message({
               type: 'info',
               message: '已取消执行！'
             })
           })
       }
      },

      //重置
      reset() {
        this.formInline.page = 1
        this.formInline.taskName = ""
        this.getList(this.formInline)
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getList(this.formInline)
      },
      /////////////////////////////////////////////////////////////////////
      addTask() {
        this.visible = true;

      },





      /*

            //显示编辑界面
            handleEdit: function(index, row) {
              this.editFormVisible = true
              if (row != undefined && row != 'undefined') {
                this.title = '修改任务'
                this.editForm.id = row.id

              } else {
                this.isShow = false
                this.isShow1 = false
                this.title = '新增任务'
                this.editForm.id = ''
                this.editForm.taskName = ''
                this.editForm.example = ''
                this.editForm.sourceType = ''
                this.editForm.sourceId = ''
                this.editForm.datasourceIds =''
              }
            },
            // 编辑、添加提交方法
            submitForm(editData) {
              var cur_id = this.editForm.example;
              var cur_e = "";
              this.editForm.modelTargets = []
              for(var i = 0; i < this.exampleData.length; i++){
                if(this.exampleData[i].id == this.editForm.example){
                  cur_e = this.exampleData[i].example
                }
              }
              this.editForm.modelTargets[0] = {
                id:cur_id,
                example:cur_e
              }
              if(this.editForm.sourceType == "非结构化数据源"){
                this.editForm.datasourceIds = []
                for(var i =0;i<this.checkedData.length;i++){
                  this.editForm.datasourceIds[i]= this.checkedData[i]["id"]
                }
              }
              console.log("this.editForm.datasourceIds",this.editForm.datasourceIds)
              let config = {
                headers:
                  {
                    'Content-Type':'application/json',
                    'user' : localStorage.getItem('userId')
                  }
              }
              this.$refs[editData].validate(valid => {
                if (valid) {
                  console.log("fffffffffff",this.editForm)
                  // 请求方法
                  axios
                    .post(this.$global.javaUrl+"/task/saveOrUpdate",this.editForm,config)
                    .then(res => {
                      this.editFormVisible = false
                      this.loading = false
                      if (res.data.data != null) {
                        this.$message({
                          type: 'success',
                          message: "数据保存成功！"
                        })
                        this.getSearchPage()
                      } else {
                        this.$message({
                          type: 'info',
                          message: res.msg
                        })
                      }
                    })
                    .catch(err => {
                      this.editFormVisible = false
                      this.loading = false
                      this.$message.error('保存失败，请稍后再试！')
                    })
                } else {
                  return false
                }
              })

            },
      */


      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val
      },






    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }
  .userRole {
    width: 100%;
  }
</style>
